package com.car.customer.controller;


import com.car.customer.domain.SysCarCustomer;
import com.car.customer.service.SysCarCustomerService;
import com.car.customer.util.CustomCaptchaGenerator;
import com.car.customer.util.Result;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@CrossOrigin(origins = "http://localhost:8080", allowCredentials = "true")
@RestController
@RequestMapping("customer")
public class CustomerController {


    @Autowired
    MinioClient minioClient;

    @Value(value = "${minio.bucket-name}")
    private String bucket;

    @Value(value = "${minio.url}")
    private String url;


    @Autowired
    SysCarCustomerService carCustomerService;




    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("login")
    public Result login(@RequestBody SysCarCustomer carCustomer) {

       SysCarCustomer sysCarCustomer =  carCustomerService.selectLoginCustomer(carCustomer.getCustomerName());

        if (carCustomer == null) {
            return Result.fail("账号或密码错误");
        }
        if (!carCustomer.getLoginPassword().equals(sysCarCustomer.getLoginPassword())) {
            return Result.fail("账号或密码错误");
        }
        if (!CustomCaptchaGenerator.getGeneratedCaptchaText().equals(carCustomer.getCode())) {
            return Result.fail("验证码错误");
        }
        return Result.success();
    }


    @PostMapping("customerUpload")
    public Result customerUpload(MultipartFile file) {

      String fileName =   UUID.randomUUID()+"_"+file.getOriginalFilename();

        try {

            minioClient.putObject(PutObjectArgs.builder()
                            .object(fileName)
                            .bucket(bucket)
                            .stream(file.getInputStream(), file.getSize(), -1)
                            .contentType(file.getContentType())
                    .build());
            return Result.success(url+"/"+bucket+"/"+fileName);

        } catch (Exception a) {
            a.printStackTrace();
            return Result.fail();
        }
    }

    @GetMapping("CustomerGenerator")
    public Result CustomerGenerator(String uid) {
        String imageCode = CustomCaptchaGenerator.generateCaptcha();
        redisTemplate.opsForValue().set(uid,CustomCaptchaGenerator.getGeneratedCaptchaText(),2, TimeUnit.MINUTES);
        return Result.success(imageCode);
    }


    @GetMapping("CustomerList")
    public Result customerList() {
        List<SysCarCustomer> list =  carCustomerService.customerList();
        return Result.success(list);
    }


    @GetMapping("CustomerDel")
    public Result customerDel(Integer id) {
        Boolean del = carCustomerService.customerDel(id);
        return Result.success(del);
    }

    @PostMapping("CustomerUpdata")
    public Result customerUpdata(@RequestBody SysCarCustomer carCustomer) {
        Boolean updata = carCustomerService.customerUpdata(carCustomer);
        return Result.success(updata);
    }

    @PostMapping("CustomerInsert")
    public Result customerInsert(@RequestBody SysCarCustomer carCustomer) {
        Boolean insert = carCustomerService.customerInsert(carCustomer);
        return Result.success(insert);
    }

}
